//
//  HomePageViewController.m
//  DaMaoMall
//
//  Created by HXHL-wally on 16/7/8.
//  Copyright © 2016年 daMao-wally. All rights reserved.
//

#import "HomePageViewController.h"
#import "HomePageViewModel.h"
#import "HomePageCell.h"
#import "DMCycleScrollView.h"
#import "ButtonScrollView.h"
#import "SearchViewController.h"
#import "DetailViewController.h"

@interface HomePageViewController () <DMCycleScrollViewDelegate,ButtonScrollViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray * modelArray;//model 数据
@property (nonatomic, strong) NSMutableArray * collectionArray;//collection 数据
@property(nonatomic,strong) MoreViewController *moreViewController;/**<更多界面控制器*/
@property(nonatomic,strong) WJXCollectionView *collectionView;/**<九宫格collectionview*/
@property (nonatomic,strong) NSString * titles;//collectionViewCell的title
@property (nonatomic,strong) NSArray * titlesArray;//横向滚动scrollview title

@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    self.titleLabel.hidden = YES;
    self.leftBtn.hidden = YES;
    
    [self.navigationController.tabBarItem setBadgeValue:@"3"];
    
    [self setUpSearchBar];
    
    [self setUpHomePageBannar];
    
    [self setUpScrollView];
    
//    [self showHomePageData];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [self setUpCollectionView];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    if (_collectionView != nil)
    {
        [_collectionView removeFromSuperview];
        _collectionView = nil;
    }
    if (_moreViewController != nil)
    {
        _moreViewController = nil;
    }
}

#pragma makr - 创建搜索框
- (void)setUpSearchBar
{
    
    UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(15, 28, GetScreenWidth-30, 30)];
    textField.backgroundColor = [UIColor whiteColor];
    textField.layer.borderWidth = 1;
    textField.layer.cornerRadius = 5;
    textField.layer.borderColor = RGBA(192, 192, 192, 1).CGColor;
    textField.delegate = self;
    
    //创建放大镜的view
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35, 30)];
    textField.leftView.frame = CGRectMake(5, 0, 30,30);
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    UIImage * image = [UIImage imageNamed:@"bar_search"];
    UIImageView * imageview  = [[UIImageView alloc]initWithImage:image];
    imageview.frame = CGRectMake(5, 3, image.size.width, image.size.height);
    [view addSubview:imageview];
    textField.leftView = view;
    textField.placeholder =  @"请在此处输入";
    textField.font = [UIFont systemFontOfSize:14];
    textField.clearsOnBeginEditing = YES;
    textField.clearButtonMode = YES;
    
    [self.navigationBarView addSubview:textField];
    
}
#pragma mark - textField代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
{
    DMLog(@"我要搜索了");
    SearchViewController * search = [[SearchViewController alloc]init];
    
    [self.navigationController pushViewController:search animated:YES];
    
    return NO;
}

#pragma mark - 首页轮播图
- (void)setUpHomePageBannar
{
    NSArray * imagesURLString = @[
                                  @"http://image.damao.cn/uploadimage/201606/24/15-31-00-70-29338.jpg",
                                  @"http://image.damao.cn/uploadimage/201607/05/17-06-37-66-29752.jpg",
                                  @"http://image.damao.cn/uploadimage/201607/14/09-56-04-20-29752.jpg",
                                  @"http://image.damao.cn/uploadimage/201607/14/09-58-32-21-29752.jpg",
                                  @"http://image.damao.cn/uploadimage/201607/14/11-28-18-52-29752.jpg"
                                  ];
    
      // 广告字
//    NSArray * titles = @[
//                         @"我是第一",
//                         @"我是第二",
//                         @"我是第三"];
    
    // 网络加载 --- 创建带标题的图片轮播器
    DMCycleScrollView * cycleScrollView = [DMCycleScrollView DMCycleScrollViewWithFrame:CGRectMake(0, 64, GetScreenWidth, 150) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
    cycleScrollView.pageControlAliment = DMCycleScrollViewPageContolAlimentLeft;
    cycleScrollView.pageControlStyle = DMCycleScrollViewPageContolStyleAnimated;
//    cycleScrollView.titlesGroup = titles;
    cycleScrollView.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    [self.view addSubview:cycleScrollView];
    
    //         --- 模拟加载延迟
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cycleScrollView.imageURLStringsGroup = imagesURLString;
    });
    
    // block监听点击方式
    
//    cycleScrollView.clickItemOperationBlock = ^(NSInteger index) {
//        NSLog(@">>>>>  %ld", (long)index);
//    };
}
#pragma mark - DMCycleScrollViewDelegate 点击图片回调

- (void)DMCycleScrollView:(DMCycleScrollView *)DMCycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    DMLog(@"---点击了第%ld张图片", (long)index);
    DetailViewController * detail = [[DetailViewController alloc]init];
    detail.titles = @"广告";
    [self.navigationController pushViewController:detail animated:YES];
}

#pragma mark -  创建scrollView
- (void)setUpScrollView
{
    NSArray * imageArray = @[@"findhome_0.png",@"findhome_1.png",@"findhome_2.png",@"findhome_3.png",@"findhome_4.png",@"findhome_5.png"];
    self.titlesArray = @[@"大猫推优",@"招采信息",@"品牌专区",@"找加工",@"找材料",@"找资金"];
    ButtonScrollView * scrollView = [ButtonScrollView ButtonScrollViewWithFarme:CGRectMake(0, 224, GetScreenWidth, 110) delegate:self ImageURLStringsGroup:imageArray titlesGroup:self.titlesArray];
    scrollView.buttonScrollViewDelegate = self;
    [self.view addSubview:scrollView];
}

- (void)didSelectScrollButtonAtIndex:(NSInteger)index{
    DMLog(@"%ld",index);
    NSInteger title = index - 1000;
    DetailViewController * detail = [[DetailViewController alloc]init];
    detail.titles = [self.titlesArray objectAtIndex:title];
    [self.navigationController pushViewController:detail animated:YES];
    
}

#pragma mark - collectionview
- (void)setUpCollectionView
{
    NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"mainGrid.plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:filepath];
    if (array == nil)
    {
        NSString *filepath = [[NSBundle mainBundle] pathForResource:@"mainGrid.plist" ofType:nil];
        array = [NSArray arrayWithContentsOfFile:filepath];
    }
    _collectionArray = array.mutableCopy;
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"mainGrid.plist"];
    
    CGRect frame = CGRectMake(0, 344, kScreenWidth,kScreenHeight);
    _collectionView = [MoveCollectionHandle MoveCollectionHandle:self withView:self.view withData:_collectionArray withFrame:frame numOfItems:4  OperateType:DemonOperateTypeDelete  DelectBtnBlock:^(NSIndexPath *indexPath)
                       {
                           NSDictionary *dic = _collectionArray[indexPath.row];
                           self.moreViewController.Datadic = dic;
                           [_collectionArray removeObjectAtIndex:indexPath.row];
                           [_collectionArray writeToFile:path atomically:YES];
                           
                           
                       } moveEndBlock:^(NSIndexPath *beginIndex, NSIndexPath *endIndex)
                       {
                           id dataModel = _collectionArray[beginIndex.row];
                           [_collectionArray removeObjectAtIndex:beginIndex.row];
                           [_collectionArray insertObject:dataModel atIndex:endIndex.row];
                           [_collectionArray writeToFile:path atomically:YES];
                           
                       } selectedBtnBlock:^(NSIndexPath *selectedIndex)
                       {
                           if (selectedIndex.row == _collectionArray.count-1)
                           {
                               __block typeof(self)blockSelf = self;
                               self.moreViewController.addBlock = ^(NSDictionary *addDictionary){
                                   [blockSelf.collectionArray insertObject:addDictionary atIndex:_collectionArray.count-1];
                                   [blockSelf.collectionArray writeToFile:path atomically:YES];
                               };
                               
                               [self.navigationController pushViewController:self.moreViewController animated:YES];
                               return ;
                           }
                           else
                           {
                               NSLog(@"index:%ld",selectedIndex.row);
                               for (id f in [_collectionArray objectAtIndex:selectedIndex.row]) {
                                   self.titles = f;
                               }
                               DetailViewController * detail = [[DetailViewController alloc]init];
                               detail.titles = self.titles;
                               [self.navigationController pushViewController:detail animated:YES];
                               
                           }
                       }];
    
}
#pragma mark -
#pragma mark set & get
- (MoreViewController *)moreViewController
{
    if (!_moreViewController)
    {
        _moreViewController = [[MoreViewController alloc] initWithNuberItems:4];
    }
    return _moreViewController;
}


/*
#pragma mark - 获取首页数据
- (void)showHomePageData
{
    HomePageViewModel * ViewModel = [[HomePageViewModel alloc] init];
    [ViewModel setBlockWithReturnBlock:^(id returnValue) {
        
        [SVProgressHUD dismiss];
        _modelArray = returnValue;
        [self.tableView reloadData];
        DMLog(@"%@",_modelArray);
        
    } WithErrorBlock:^(id errorCode) {
        
        [SVProgressHUD dismiss];
        
    } WithFailureBlock:^{
        
        [SVProgressHUD dismiss];
        
    }];
    // 获取首页数据
    [ViewModel fetchHomePageData];
    
//    [SVProgressHUD showWithStatus:@"正在获取数据" maskType:SVProgressHUDMaskTypeBlack];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomePageCell" forIndexPath:indexPath];
    
    [cell setValueWithDic:_modelArray[indexPath.row]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomePageViewModel * publicViewModel = [[HomePageViewModel alloc] init];
//    [publicViewModel weiboDetailWithPublicModel:_modelArray[indexPath.row] WithViewController:self];
}

*/


@end